package com.phone.html5.document;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.phone.html5.Html5Action;
import com.xt.base.wireless.server.MobileActionUtil;

public class DocumentAction extends Html5Action{

	private static final long serialVersionUID = 8151511874055280098L;
	private int historyCount=0;
	private int historyCountStep=0;
	private String comment;
	/**
	 * 功  能：获取待办文件
参  数： 
　　　User：用户全名
　　　Password：用户密码
　　　Filetype：文件类型（收文、发文）
　　　StartPos：起始页
　　　Count：条数
返回值：
　　成功：待办文件总数目@邮箱库notsid@文件类型@待办文件信息列表，列表信息使用”#”分割每个待办文件，
每个文件使用”|”分割各项信息，包括：文件ID、发送时间、发送人、标题
失败：ERROR

(GetWaitFile)?OpenAgent&user=<用户全名>&password=<用户密码>&filetype=<文件类型>&startpos=<起始位置>&count=<条数>
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String documentDaibanList(){
		this.putBaseParam();
		paramMap.put("filetype", 1);
		paramMap.put("count", defaultpagesize);
		paramMap.put("startpos", (pageNumber-1)*defaultpagesize);
		xmlString = this.myHttpClient.request("(GetWaitFile)?OpenAgent&"+this.getBaseParamString());
		//resultMap = MobileActionUtil.parseRequestXml(xmlString);
		this.dealPageNumber((List<Object>)resultMap.get("list"));
		resultString = "documentDaibanList.jsp";
		return "resultString";
	}
	public String getContent(){
		this.putBaseParam();
		paramMap.put("code", 2);
		if("documentDaiyueList".equals(this.operType)){
			paramMap.put("funcid", "B002");
		}else{
			paramMap.put("funcid", "B001");
		}
		
		/*String s = this.getParam("id");
		s = s==null || s.isEmpty() ?this.getParam("universalid"):s;*/
		//paramMap.put("universalid", universalid);
		//xmlString = this.approveService.getApproveInfo(paramMap);
		resultMap = MobileActionUtil.parseRequestXml(xmlString);
		resultString = "documentContent.jsp";
		this.historyCount += this.historyCountStep;
		return "resultString";
	}
	@SuppressWarnings("unchecked")
	public String queryDocument(){
		this.putBaseParam();
		paramMap.put("code", 1);
		paramMap.put("defaultpagesize", defaultpagesize);
		paramMap.put("pagenumber", pageNumber);
		paramMap.put("funcid", "B005");
		if("session".equals(scope)){
			paramMap.put("type", this.getSessionAttr("type"));
			paramMap.put("worktype", this.getSessionAttr("worktype"));
			paramMap.put("keyword", this.getSessionAttr("keyword"));
			paramMap.put("wftype",  this.getSessionAttr("wftype"));
		}else{
			String s = this.getParam("type");
			this.setSessionAttr("type", s);
			paramMap.put("type", s);
			
			s = this.getParam("worktype");
			this.setSessionAttr("worktype", s);
			paramMap.put("worktype", s);
			
			s = this.getParam("keyword");
			this.setSessionAttr("keyword", s);
			paramMap.put("keyword", s);
		
			String[] wftypes = this.getRequest().getParameterValues("wftype");
			String wftype;
			if(wftypes==null){
				wftype = this.getParam("wftype");
			}else{
				wftype = wftypes.length==2?wftypes[0]+","+wftypes[1]:wftypes[0];
			}
			paramMap.put("wftype", wftype);
		}
		//xmlString = this.approveService.queryApproveList(paramMap);
		resultMap = MobileActionUtil.parseRequestXml(xmlString);
		this.dealPageNumber((List<Object>)resultMap.get("list"));
		resultString = "documentDaibanList.jsp";
		return "resultString";
	}
	@SuppressWarnings("unchecked")
	public String documentDaiyueList(){
		this.putBaseParam();
		paramMap.put("code", 1);
		paramMap.put("defaultpagesize", defaultpagesize);
		paramMap.put("pagenumber", pageNumber);
		paramMap.put("funcid", "B002");
		//xmlString = this.approveService.queryApproveList(paramMap);
		resultMap = MobileActionUtil.parseRequestXml(xmlString);
		this.dealPageNumber((List<Object>)resultMap.get("list"));
		resultString = "documentDaiyueList.jsp";
		return "resultString";
	}
	
	public String zhihui(){
		List<String> list = new ArrayList<String>();
		String s = this.getParam("sendto");
		if(s!=null && !s.equals("")){
			list = Arrays.asList(s.split(","));
		}else{
			list.add("");
		}
		this.putBaseParam();
		paramMap.put("code", 2);
		paramMap.put("dbpath", "softoon\\Workflow\\WorkFlow\\WorkFlow.nsf");
		paramMap.put("sendto", list);
		paramMap.put("funcid", "D002");
		paramMap.put("randomid",new Random().nextLong());
		//paramMap.put("universalid", universalid);
		//xmlString = this.approveService.saveOrUpdate(paramMap);
		resultMap = MobileActionUtil.parseRequestXml(xmlString);
		this.setSessionAttr(this.operType4Session, "zhihui");
		return getContent();
	}
	public String approve(){
		this.putBaseParam();
		paramMap.put("code", 4);
		paramMap.put("activetype", this.getParam("activetype"));
		paramMap.put("comment", "<![CDATA["+this.getParam("comment")+"]]>");
		paramMap.put("date", new Date().getTime());
		paramMap.put("funcid", "B001");
		paramMap.put("randomid",new Random().nextLong());
		//paramMap.put("universalid", universalid);
		//xmlString = this.approveService.saveOrUpdate(paramMap);
		resultMap = MobileActionUtil.parseRequestXml(xmlString);
		this.setSessionAttr(this.operType4Session, "pishi");
		return getContent();
	}
	
	public String documentSubmit(){
		this.putBaseParam();
		paramMap.put("code",3);
		paramMap.put("activetype", this.getParam("activetype"));
		paramMap.put("comment", "<![CDATA["+this.getParam("comment")+"]]>");
		paramMap.put("date", new Date().getTime());
		paramMap.put("funcid", "B001");
		paramMap.put("randomid",new Random().nextLong());
		//paramMap.put("universalid", universalid);
		//xmlString = this.approveService.saveOrUpdate(paramMap);
		resultMap = MobileActionUtil.parseRequestXml(xmlString);
		this.set("submit", "yes");
		this.setSessionAttr(this.operType4Session, "submit");
		return getContent();
	}
	public String documentRead(){
		this.putBaseParam();
		paramMap.put("code",3);
		paramMap.put("activetype", this.getParam("activetype"));
		paramMap.put("readed", "true");
		paramMap.put("date", new Date().getTime());
		paramMap.put("funcid", "B002");
		paramMap.put("randomid",new Random().nextLong());
		//paramMap.put("universalid", universalid);
		//xmlString = this.approveService.saveOrUpdate(paramMap);
		resultMap = MobileActionUtil.parseRequestXml(xmlString);
		this.set("readed", "yes");
		return getContent();
	}
	public int getHistoryCount() {
		return historyCount;
	}
	public void setHistoryCount(int historyCount) {
		this.historyCount = historyCount;
	}
	public int getHistoryCountStep() {
		return historyCountStep;
	}
	public void setHistoryCountStep(int historyCountStep) {
		this.historyCountStep = historyCountStep;
	}
	public String getComment() {
		return comment;
	}
	public void setComment(String comment) {
		this.comment = comment;
	}
}
